package com.abiao.shop.service;

import com.abiao.shop.dao.OrderDao;
import com.abiao.shop.pojo.DiscountOrder;
import com.abiao.shop.pojo.User;
import com.abiao.shop.redis.OrderKey;
import com.abiao.shop.redis.RedisService;
import com.abiao.shop.vojo.GoodsVo;
import com.abiao.shop.vojo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: OrderService   订单业务操作类
 * @Author: AB
 * @Date: 2021-07-17
 * @Version:v1.0
 */
@Service
public class OrderService {

    @Autowired
    OrderDao orderDao;
    @Autowired
    RedisService redisService;

    /**
     * 获取用户对某个商品的秒杀订单
     * @param userId
     * @param goodsId
     * @return
     */
    public DiscountOrder getDCOrderByGoodsIdUserId(long userId, long goodsId) {
        DiscountOrder order = redisService.get(OrderKey.getDiscountOrderByUidGid,""+userId+"_"+goodsId,DiscountOrder.class);
        if (order == null){
            order = orderDao.getOrderByGoodsIdUserId(userId,goodsId);
            redisService.set(OrderKey.getDiscountOrderByUidGid,""+userId+"_"+goodsId,order);
        }
        return order;
    }
    /**
     * 从缓存获取用户对某个商品的秒杀订单
     * @param userId
     * @param goodsId
     * @return
     */
    public DiscountOrder getDCOrderByGoodsIdUserIdInCache(long userId, long goodsId) {
        return redisService.get(OrderKey.getDiscountOrderByUidGid,""+userId+"_"+goodsId,DiscountOrder.class);
    }

    /**
     * 生成普通订单和秒杀订单
     * @param user
     * @param goodsVo
     * @return
     */
    @Transactional
    public OrderVo createOrder(User user, GoodsVo goodsVo) {
        OrderVo order = new OrderVo();

        order.setStatus(0);
        order.setCreateDate(new Date());
        order.setDeliveryAddrId(0);
        order.setOrderChannel(1);
        //goods
        order.setGoodsId(goodsVo.getId());
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(goodsVo.getDiscountPrice());
        //user
        order.setUserId(user.getId());
        //插入普通订单到数据库
        orderDao.insertOrder(order);

        DiscountOrder discountOrder = new DiscountOrder();
        discountOrder.setGoodsId(goodsVo.getId());
        discountOrder.setUserId(user.getId());
        discountOrder.setOrderId(order.getId());
        //插入秒杀订单到数据库
        orderDao.insertDiscountOrder(discountOrder);
        //秒杀订单写入缓存
        redisService.set(OrderKey.getDiscountOrderByUidGid,""+user.getId()+"_"+goodsVo.getId(),discountOrder);

        order.setGoodsImg(goodsVo.getGoodsImg());
        //生成订单写入缓存
        redisService.set(OrderKey.getOrderByUidGid,""+user.getId()+"_"+goodsVo.getId(),order);
        return order;
    }

    /**
     * 获取用户对某个商品的普通订单
     * @param userId
     * @param goodsId
     * @return
     */
    public OrderVo getOrderByGoodsIdUserId(long userId, long goodsId) {
        return redisService.get(OrderKey.getOrderByUidGid,""+userId+"_"+goodsId,OrderVo.class);
    }

    /**
     * 获取用户所有订单
     * @param user
     * @return
     */
    public List<OrderVo> getUserAllOrders(User user){
        return orderDao.getUserAllOrders(user);
    }

    /**
     * 支付订单
     * @param orderId
     */
    public void payOrder(long orderId){
        orderDao.payOrder(orderId);
    }
}
